/*
 * Copyright (c) 2020.
 */

package com.jojo.android.zxlib.tool;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;


import java.io.File;


public
class ZxImageUpload {

    /**
     * 图片最大宽或高
     */
    public static final int MAX_SIZE = 1280;


    private static Bitmap getBitmapByFile(String filePath) {
        File f;
        BitmapFactory.Options options;
        try {
            if (null != filePath && !"".equals(filePath)) {
                f = new File(filePath);
                if (f.exists() && f.isFile()) {
                    options = new BitmapFactory.Options();
                    options.inJustDecodeBounds = true;
                    BitmapFactory.decodeFile(filePath, options);
                    options.inSampleSize = calculateInSampleSize(options);
                    options.inJustDecodeBounds = false;
                    return BitmapFactory.decodeFile(filePath, options);
                }
            }
        } catch (Exception ignored) {
        }
        return null;
    }

    /**
     * 计算采样
     *
     * @param options
     * @return
     */
    private static int calculateInSampleSize(BitmapFactory.Options options) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
        if (height > MAX_SIZE || width > MAX_SIZE) {
            final int heightRatio = Math.round((float) height / (float) MAX_SIZE);
            final int widthRatio = Math.round((float) width / (float) MAX_SIZE);
            inSampleSize = heightRatio > widthRatio ? heightRatio : widthRatio;
            if (inSampleSize < 2) {
                inSampleSize = 1;
            }
        }
        return inSampleSize;
    }


    /**
     * 将位图按一定规则压缩。
     *
     * @param src
     * @return
     */
    public static Bitmap compressImage(Bitmap src) {
        Bitmap target = null;
        //如果图片宽和高同时小于最大长度
        if (src.getHeight() < MAX_SIZE && src.getWidth() < MAX_SIZE) {
            target = src;
        } else {
            float wh = ZxImageTool.getImageW_HScale(src);
            //如果宽高比大于二
            if (wh > 2) {
                //如果宽和高同时大于指定的最大长度
                if (src.getHeight() > MAX_SIZE && src.getWidth() > MAX_SIZE) {
                    //如果宽大于高
                    if (src.getWidth() > src.getHeight())
                        target = ZxImageTool.zoomImage(src, MAX_SIZE * wh, MAX_SIZE);
                    else
                        target = ZxImageTool.zoomImage(src, MAX_SIZE, MAX_SIZE / wh);
                } else {
                    target = src;
                }
            } else {
                target = ZxImageTool.compressByMaxSize(src, MAX_SIZE, true);
            }
        }
        target = ZxImageTool.compressByQuality(target, 100, true);
        return target;
    }


    /**
     * 根据文件路径获取按一定规则压缩的位图
     *
     * @param filepath
     * @return
     */
    public static Bitmap getCompressBitmap(String filepath) {
        Bitmap bitmap = getBitmapByFile(filepath);
        return ZxImageTool.compressByQuality(bitmap, 50, true);
    }

    /**
     * 压缩后上传图片
     *
     * @param path 路径
     * @return
     */
    public static String uploadImage(String url,String path) {
        Bitmap b = null;
        try {
            File file = new File(path);
            String suffix = ".jpg";
            if (file.exists()){
                suffix = file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf('.'));
            }
            b = getCompressBitmap(path);
            File img = File.createTempFile("" + System.currentTimeMillis(), suffix);
            ZxFileTool.writeBitmapToSD(img.getPath(), b, true);
            String result = ZxUploadTool.uploadFile(url, img, new ZxUploadTool.ProgressListener() {
                @Override
                public void onProgress(long totalBytes, long remainingBytes, boolean done) {
                    ZxLogTool.i("上传中:", "" + remainingBytes + "/" + totalBytes);
                }
            },"file",null);

            return result;
        } catch (Exception e) {
            e.printStackTrace(System.err);
        } finally {
            if (b != null && !b.isRecycled()) {
                b.recycle();
            }
        }
        return null;
    }


}
